using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace xingzhimeng
{
  ///<summary>
  ///
  ///</summary>
public class Birds : BirdParent
  {
    private Transform trans;
    private float Distance=1.5f;//弹弓拉扯距离
    private SpringJoint2D Joint;
    protected Rigidbody2D _rigidbody2D;
    private Transform RightPoint;//左右弹弓点
    private Transform LeftPoint;
    private LineRenderer RightLR;//左右弹弓划线
    private LineRenderer LeftLR;
    private bool isFly;//是否飞出
    private bool isDrag;//拖拽
    private bool Drag=true;//控制小鸟的拖拽次数，当发出时便不能拖拽；
    public GameObject BirdBool;//爆炸特效
    public GameObject GameWinScore;//小鸟胜利得分
    protected float DistorTime = 4;
    float x;//控制相机移动属性
    public float CameraSpeed=20;
    private void Awake()
    {

      trans = transform.parent.parent.Find("FiringPoint");
     Joint = GetComponent<SpringJoint2D>();
     _rigidbody2D = GetComponent<Rigidbody2D>();
     RightPoint = transform.parent.parent.Find("Right");
     LeftPoint = transform.parent.parent.Find("Left");
     RightLR = RightPoint.GetComponent<LineRenderer>();
     LeftLR = LeftPoint.GetComponent<LineRenderer>();

    }

    private void Update()
    {
      if (isDrag&&Drag)
      {
        transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);
        if (Vector3.Distance(trans.position, transform.position) >Distance)
        {
          transform.position = ((transform.position-trans.position).normalized) * Distance+trans.position;
      
        }
        Line();
      }
      //控制相机跟随小鸟
             x = transform.position.x;
            Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,new Vector3( Mathf.Clamp(x,-3,10)
                ,Camera.main.transform.position.y, Camera.main.transform.position.z),CameraSpeed*Time.deltaTime);
            //小鸟特技
            if (isFly && Input.GetMouseButtonDown(0)&&!EventSystem.current.IsPointerOverGameObject())
            {
                MouseClick();
            }
        }


        protected virtual void MouseClick()
        {
            //小鸟特技
            isFly = false;
        }

       

 
//弹弓划线
    void Line()
    {
     RightLR.SetPosition(0,RightPoint.position);
     RightLR.SetPosition(1,transform.position);
     LeftLR.SetPosition(0,LeftPoint.position);
     LeftLR.SetPosition(1,transform.position);
    }

    protected virtual void OnMouseUp()
    {
      if (Drag==false) return;
      isDrag = false;
      Drag = false;
      isFly = true;
            //播放声音
            Audios.Instance.PlayClip(transform.position, ClipName.birdFly);
            //相机跟随  
      Joint.enabled = false;
      //禁用弹弓弹簧
      RightLR.enabled = false;
      LeftLR.enabled = false;
      Invoke("Dead",DistorTime);
    }
 
       protected virtual void Dead()
        {
            GameObject bl = Instantiate(BirdBool, transform.position, Quaternion.identity);
            Destroy(bl, 0.3f);
            GameManeger.Insnter.NextBird(this);
            //播放消失声音
            Audios.Instance.PlayClip(transform.position, ClipName.birdDead);
            Destroy(gameObject);
        }

    private void OnMouseDown()
    {
      if (Drag==false) return;
      isDrag = true;
      Audios.Instance.PlayClip(transform.position, ClipName.bird1);
      RightLR.enabled = true;
      LeftLR.enabled = true;
      // _rigidbody2D.isKinematic = true;
    }

        protected virtual void OnCollisionEnter2D(Collision2D collision)
        {//碰到物体后点击不触发特技
            isFly = false; 
        }
    }

}

